package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * @AUTHOR: taotao
 * @DATE: 2024/1/10 16:36
 * @Description:
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Override
    public Page<HospitalSet> findPageHospSet(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hosName = hospitalSetQueryVo.getHosname();
        String hosCode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosName)) {
            wrapper.like("hosname", hosName);
        }
        if (!StringUtils.isEmpty(hosCode)) {
            wrapper.eq("hoscode", hosCode);
        }
        Page<HospitalSet> hospitalSetPage = page(page, wrapper);
        return hospitalSetPage;
    }

    @Override
    public boolean saveHospitalSet(HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random = new Random();
        String signKey = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(100));
        hospitalSet.setSignKey(signKey);
        boolean save = save(hospitalSet);
        return save;
    }
}
